package cn.stylefeng.guns.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.dictmap.BlogDict;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.system.entity.Blog;
import cn.stylefeng.guns.modular.system.model.BlogDto;
import cn.stylefeng.guns.modular.system.service.BlogService;
import cn.stylefeng.guns.modular.system.warpper.BlogWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/blog")
public class BlogController  extends BaseController {
    private String PREFIX = "/modular/system/blog/";

    @Autowired
    private BlogService blogService;

    /**
     * 跳转到博客管理首页
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "blog.html";
    }

    /**
     * 跳转到添加博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping("/blog_add")
    public String blogAdd() {
        return PREFIX + "blog_add.html";
    }

    /**
     * 跳转到修改博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @Permission
    @RequestMapping("/blog_update")
    public String blogUpdate(@RequestParam("blogId") Long blogId) {

        if (ToolUtil.isEmpty(blogId)) {
            throw new RequestEmptyException();
        }

        //缓存博客修改前详细信息
        Blog blog = blogService.getById(blogId);
        LogObjectHolder.me().set(blog);

        return PREFIX + "blog_edit.html";
    }

    /**
     * 获取博客的tree列表，ztree格式
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.blogService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }



    /**
     * 新增博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:57 PM
     */
    @BussinessLog(value = "添加博客", key = "title", dict = BlogDict.class)
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public ResponseData add(Blog blog) {
        this.blogService.addBlog(blog);
        return SUCCESS_TIP;
    }

    /**
     * 获取所有博客列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:57 PM
     */

    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(value = "condition", required = false) String condition) {
        Page<Map<String, Object>> list = this.blogService.list(condition);
        Page<Map<String, Object>> wrap = new BlogWarpper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 博客详情
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:57 PM
     */
    @RequestMapping(value = "/detail/{blogId}")
    @Permission
    @ResponseBody
    public Object detail(@PathVariable("blogId") Long blogId) {
        Blog blog = blogService.getById(blogId);
        BlogDto blogDto = new BlogDto();
        BeanUtil.copyProperties(blog, blogDto);
        return blogDto;
    }

    /**
     * 修改博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:57 PM
     */
    @BussinessLog(value = "修改博客", key = "simpleName", dict = BlogDict.class)
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public ResponseData update(Blog blog) {
        blogService.editBlog(blog);
        return SUCCESS_TIP;
    }

    /**
     * 删除博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:57 PM
     */
    @BussinessLog(value = "删除博客", key = "id", dict = BlogDict.class)
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public ResponseData delete(@RequestParam long blogId) {

        //缓存被删除的博客名称
        LogObjectHolder.me().set(ConstantFactory.me().getBlogTitle(blogId));

        blogService.deleteBlog(blogId);

        return SUCCESS_TIP;
    }

}
